from tkinter import Tk
from tkinter import ttk
from database.table import tablee
from database.table.tablee import Supplier
import tkinter as tk

def SearchSupplier():
    search_supplier_screen = Tk()
    search_supplier_screen.title('查询供应商信息')
    screen_width, screen_height = search_supplier_screen.maxsize()
    # 窗口的宽和高
    width = 605
    height = 200
    centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                              (screen_height - height) / 2)
    search_supplier_screen.geometry(centre)
    search_supplier_screen.resizable(0, 0)

    label = tk.Label(search_supplier_screen, text='请输入要查询的供应商编号：', font=('楷体', 14))
    label.place(x=150, y=20)
    # 编号标签文本框
    user_num_label = tk.Label(search_supplier_screen, text='编号', font=('楷体', 20))
    user_num_label.place(x=150, y=60)
    user_num_text = tk.StringVar()
    user_num_entry = tk.Entry(search_supplier_screen, textvariable=user_num_text,
                              width=15)
    user_num_entry.place(x=230, y=68)

    # 确定按钮响应事件
    def confirm():
        id = str(user_num_entry.get())
        supplierr = tablee.session.query(Supplier).filter(Supplier.Sno == id).all()
        data = Supplier.to_json(supplierr)
        length = len(data)

        if length == 1:  # 查找的员工编号存在
            search = tk.Toplevel(search_supplier_screen)
            search.title(id + '供应商信息')
            screen_width, screen_height = search.maxsize()
            # 窗口的宽和高
            width = 915
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            search.geometry(centre)

            tree = ttk.Treeview(search)  # 创建表格对象
            tree["columns"] = ("编号", "名称", "简称", "地址", "电话", "邮箱", "联系人","联系人电话","备注")
            tree.column("编号", width=50)  # 设置列
            tree.column("名称", width=110)
            tree.column("简称", width=60)
            tree.column("地址", width=100)
            tree.column("电话", width=100)
            tree.column("邮箱", width=100)
            tree.column("联系人", width=50)
            tree.column("联系人电话", width=100)
            tree.column("备注", width=50)
            tree.heading("编号", text="编号")  # 显示表头名
            tree.heading("名称", text="名称")
            tree.heading("简称", text="简称")
            tree.heading("地址", text="地址")
            tree.heading("电话", text="电话")
            tree.heading("邮箱", text="邮箱")
            tree.heading("联系人", text="联系人")
            tree.heading("联系人电话", text="联系人电话")
            tree.heading("备注", text="备注")

            for i in range(0, length):
                num = data[i]['Sno']
                allname = data[i]['Sallname']
                name = data[i]['Sname']
                address = data[i]['Saddress']
                phone = data[i]['Sphone']
                email = data[i]['Semail']
                contact = data[i]['Scontact']
                conphone = data[i]['Sconphone']
                remarks = data[i]['Sremarks']
                tree.insert("", 0, text='', value=(num, allname,name, address, phone, email,contact,conphone, remarks))
            tree.pack()

            tablee.session.close()

        elif id == '':  # 员工编号为空
            search = tk.Toplevel(search_supplier_screen)
            search.title("Error")
            screen_width, screen_height = search.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            search.geometry(centre)
            label = tk.Label(search, text='编号不能为空，请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                search.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(search, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)
        else:  # 员工编号不存在
            search = tk.Toplevel(search_supplier_screen)
            search.title("Error")
            screen_width, screen_height = search.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            search.geometry(centre)
            label = tk.Label(search, text='此编号不存在,请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                search.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(search, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)

    # 返回按钮响应事件
    def back():
        search_supplier_screen.destroy()

    # 确定按钮
    user_confirm_button = tk.Button(search_supplier_screen, text='确定', font=('楷体', 12),
                                    width=10, command=confirm)
    user_confirm_button.place(x=150, y=120)
    # 返回按钮
    user_back_button = tk.Button(search_supplier_screen, text='返回', font=('楷体', 12),
                                 width=10, command=back)
    user_back_button.place(x=280, y=120)

    search_supplier_screen.mainloop()

if __name__ == '__main__':
    SearchSupplier()